<template>
<div>
 <el-card class="box-card">
    <el-breadcrumb separator="/">
    <el-breadcrumb-item ><a href="https://element.eleme.io" >首页</a></el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/home'}">主页</el-breadcrumb-item>
    <el-breadcrumb-item >房源管理</el-breadcrumb-item>
    </el-breadcrumb><br />

    <el-row :gutter="20">
         <el-col :span="9">
            <!-- 3.定义搜索框 -->
            <el-input placeholder="搜索关键字查找相关房源" v-model="queryInfo.query" >
                <el-button slot="append" icon="el-icon-search" @click="getHouseResourcesList"></el-button>
            </el-input>
         </el-col>
         <el-col :span="4">
         <el-button type="primary" icon="el-icon-edit" @click="dialogVisible=true">添加房源</el-button>
         </el-col>
       </el-row><br />

    <!-- 定义表格 -->
    <div class="table-box">
    <el-table
    :data="houseResourceList"
    style="width: 100%"
    :row-style="{height:'60px'}"
    :cell-style="{padding:'0px'}"
    stripe>

   <!-- 定义房源列表 -->
    <el-table-column label="房源ID" prop="id" align= "center" width="100%"></el-table-column>
    <el-table-column label="房源卖点" prop="title" width="350%"></el-table-column>
    <el-table-column label="所在城市" prop="city" align="center" ></el-table-column>
    <el-table-column label="小区名称" prop="cellName" align="center" ></el-table-column>
    <el-table-column label="户型" prop="houseType" align="center" ></el-table-column>
    <el-table-column label="面积" prop="measure" align="center" width="100%"></el-table-column>
    <el-table-column label="价格" prop="price" align="center" width="100%"></el-table-column>
    <el-table-column label="在售/下架" prop="switchStatus"  align="center" width="100%">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.switchStatus" active-color="#13ce66" inactive-color="#ff4949"
           @change="updateStatus(scope.row)"></el-switch>
        </template>
    </el-table-column>
    <el-table-column label="操作" align="center" >
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="small" @click="updateUserBtn(scope.row)">修改</el-button>
        </template>
    </el-table-column>
    <!-- 可查看房源详情页 -->
    <el-table-column type="expand" align="center" label="下拉查看详情" width="110%">
      <template slot-scope="props">
        <el-form label-position="left" inline class="demo-table-expand">
          <el-form-item label="房源ID">
            <span>{{ props.row.id }}</span>
          </el-form-item>
          <el-form-item label="房源卖点">
            <span>{{ props.row.title }}</span>
          </el-form-item>
          <el-form-item label="所在城市">
            <span>{{ props.row.city }}</span>
          </el-form-item>
          <el-form-item label="小区名称">
            <span>{{ props.row.cellName }}</span>
          </el-form-item>
          <el-form-item label="面积">
            <span>{{ props.row.measure }}</span>
          </el-form-item>
          <el-form-item label="户型">
            <span>{{ props.row.houseType }}</span>
          </el-form-item>
          <el-form-item label="价格">
            <span>{{ props.row.price }}</span>
          </el-form-item>
          <el-form-item label="看房时间">
            <span>{{ props.row.lookTime }}</span>
          </el-form-item>
          <el-form-item label="房源状态">
            <span>{{ props.row.switchStatus }}</span>
          </el-form-item>
          <el-form-item label="详细地址">
            <span>{{ props.row.address }}</span>
          </el-form-item>
          <el-form-item label="详情描述">
            <span>{{ props.row.details }}</span>
          </el-form-item>
        </el-form>
      </template>
    </el-table-column>
  </el-table>
    </div>
        <el-pagination
             background
             @size-change="handleSizeChange"
             @current-change="handleCurrentChange"
             :current-page="queryInfo.pageNum"
             :page-sizes="[10, 15, 20]"
             :page-size="queryInfo.pageSize"
             layout="total, sizes, prev, pager, next, jumper"
             :total="total">
        </el-pagination>  
 </el-card>

    <!-- 新增房源对话框 -->
    <el-dialog title="新增房源" :gutter="20" :visible.sync="dialogVisible" width="40%" @close="closeDialog" :modal-append-to-body="false">
    <el-form :model="addHouseResources" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

            <el-form-item label="房源卖点" prop="title" >
                  <el-input v-model="addHouseResources.title"></el-input>
            </el-form-item>

      <el-row>
          <el-col span="12">
              <el-form-item label="房源城市" prop="city">
                <el-select v-model="addHouseResources.city" placeholder="请选择城市">
                <el-option label="上海" value="上海"></el-option>
                <el-option label="北京" value="北京"></el-option>
                <el-option label="广州" value="广州"></el-option>
                <el-option label="深圳" value="深圳"></el-option>
                <el-option label="杭州" value="杭州"></el-option>
                <el-option label="南京" value="南京"></el-option>
                <el-option label="成都" value="成都"></el-option>
                <el-option label="西安" value="西安"></el-option>
                <el-option label="郑州" value="郑州"></el-option>
                <el-option label="武汉" value="武汉"></el-option>
                <el-option label="青岛" value="青岛"></el-option>
                <el-option label="太原" value="太原"></el-option>
                <el-option label="石家庄" value="石家庄"></el-option>
                <el-option label="天津" value="天津"></el-option>
                <el-option label="重庆" value="重庆"></el-option>
                <el-option label="哈尔滨" value="哈尔滨"></el-option>
                <el-option label="长沙" value="长沙"></el-option>
                <el-option label="苏州" value="苏州"></el-option>
                </el-select>
              </el-form-item>
          </el-col>

          <el-col span="12">
              <el-form-item label="小区名称" prop="cellName">
                <el-input v-model="addHouseResources.cellName"></el-input>
              </el-form-item>
          </el-col>
      </el-row>

      <el-row>
          <el-col span="12">
              <el-form-item label="面积" prop="measure">
                <el-input  v-model="addHouseResources.measure"></el-input>
              </el-form-item>
          </el-col>
          <el-col span="12">
              <el-form-item label="户型" prop="houseType">
                <el-input v-model="addHouseResources.houseType"></el-input>
              </el-form-item>
          </el-col>
      </el-row>

      <el-row>
          <el-col span="12">
              <el-form-item label="价格" prop="price">
                <el-input v-model="addHouseResources.price"></el-input>
              </el-form-item>
          </el-col>
          <el-col span="12">
              <el-form-item label="看房时间" prop="lookTime">
                <el-input v-model="addHouseResources.lookTime"></el-input>
              </el-form-item>
          </el-col>
      </el-row>

        <el-form-item label="上传房源图片" prop="image">
            <el-upload
            action="https://jsonplaceholder.typicode.com/posts/"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
            </el-upload>
            <el-dialog :visible.sync="dialogImageVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
            </el-dialog>
        </el-form-item>

        

        <el-form-item label="详细地址" prop="address">
            <el-input v-model="addHouseResources.address"></el-input>
        </el-form-item>

        <el-form-item label="详情描述" prop="details">
            <el-input type="textarea" v-model="addHouseResources.details" placeholder="准确地描述能让房客了解到最真实的房源!"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>

    </el-form>
</el-dialog>

      <!-- 修改房源对话框 -->
      <el-dialog title="修改房源" :gutter="20" :visible.sync="dialogUpdateVisible" width="40%" @close="closeDialog" :modal-append-to-body="false">
    <el-form :model="updateHouseResources" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

            <el-form-item label="房源卖点" prop="title" >
                  <el-input v-model="updateHouseResources.title"></el-input>
            </el-form-item>

      <el-row>
          <el-col span="12">
              <el-form-item label="房源城市" prop="city">
                <el-select v-model="updateHouseResources.city" placeholder="请选择城市">
                <el-option label="上海" value="上海"></el-option>
                <el-option label="北京" value="北京"></el-option>
                <el-option label="广州" value="广州"></el-option>
                <el-option label="深圳" value="深圳"></el-option>
                <el-option label="杭州" value="杭州"></el-option>
                <el-option label="南京" value="南京"></el-option>
                <el-option label="成都" value="成都"></el-option>
                <el-option label="西安" value="西安"></el-option>
                <el-option label="郑州" value="郑州"></el-option>
                <el-option label="武汉" value="武汉"></el-option>
                <el-option label="青岛" value="青岛"></el-option>
                <el-option label="太原" value="太原"></el-option>
                <el-option label="石家庄" value="石家庄"></el-option>
                <el-option label="天津" value="天津"></el-option>
                <el-option label="重庆" value="重庆"></el-option>
                <el-option label="哈尔滨" value="哈尔滨"></el-option>
                <el-option label="长沙" value="长沙"></el-option>
                <el-option label="苏州" value="苏州"></el-option>
                </el-select>
              </el-form-item>
          </el-col>

          <el-col span="12">
              <el-form-item label="小区名称" prop="cellName">
                <el-input v-model="updateHouseResources.cellName"></el-input>
              </el-form-item>
          </el-col>
      </el-row>

      <el-row>
          <el-col span="12">
              <el-form-item label="面积" prop="measure">
                <el-input  v-model="updateHouseResources.measure"></el-input>
              </el-form-item>
          </el-col>
          <el-col span="12">
              <el-form-item label="户型" prop="houseType">
                <el-input v-model="updateHouseResources.houseType"></el-input>
              </el-form-item>
          </el-col>
      </el-row>

      <el-row>
          <el-col span="12">
              <el-form-item label="价格" prop="price">
                <el-input v-model="updateHouseResources.price"></el-input>
              </el-form-item>
          </el-col>
          <el-col span="12">
              <el-form-item label="看房时间" prop="lookTime">
                <el-input v-model="updateHouseResources.lookTime"></el-input>
              </el-form-item>
          </el-col>
      </el-row>

        <el-form-item label="详细地址" prop="address">
            <el-input v-model="updateHouseResources.address"></el-input>
        </el-form-item>

        <el-form-item label="详情描述" prop="details">
            <el-input type="textarea" :rows="4" v-model="updateHouseResources.details" placeholder="准确地描述能让房客了解到最真实的房源!"></el-input>
        </el-form-item>

        <el-form-item>
            <el-button type="primary" @click="updateHouseResourcesBtn()">立即创建</el-button>
            <el-button @click="dialogUpdateVisible=false">取消</el-button>
        </el-form-item>

    </el-form>
</el-dialog>

</div>
</template>

<script>
  export default {
    data() {
      return {
        dialogImageUrl: '',
        dialogImageVisible: false,
        dialogVisible: false,
        dialogUpdateVisible: false,
        houseResourceList:[],
        queryInfo: {
          query: '',
          pageNum: 1,
          pageSize: 10
        },
        addHouseResources: {
          title: '',
          city: '',
          cellName:"",
          measure:"",
          houseType: "",
          price: '',
          lookTime:"",
          address:"",
          details: '',
          image:""
          
        },
        updateHouseResources:{},
        rules: {
          title: [
            { required: true, message: '请填写房源卖点', trigger: 'blur' },
            { min: 8, max: 30, message: '内容为8到30个字', trigger: 'blur' }
          ],
          cellName: [
            { required: true, message: '请填写小区名称', trigger: 'blur' },
            { min: 1, max: 15, message: '内容为1到15个字', trigger: 'blur' }
          ],
          measure: [
            { required: true, message: "请填写房源面积", trigger: "blur"}
          ],
          city: [
            { required: true, message: '请选择城市', trigger: 'change' }
          ],
          houseType: [
            { required: true, message: '请填写户型', trigger: 'change' },
            { min: 1, max: 15, message: '内容为1到15个字', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '请填写房源价格', trigger: 'change' },
            { min: 1, max: 15, message: '内容为1到15个字', trigger: 'blur' }
          ],
          lookTime: [
            { required: true, message: '请填写看房时间', trigger: 'change' },
            { min: 1, max: 15, message: '内容为1到15个字', trigger: 'blur' }
          ],
          address : [
            { required: true, message: '请填写详细地址', trigger: 'change' },
            { min: 1, max: 15, message: '内容为1到15个字', trigger: 'blur' }
          ],
          details: [
            { required: true, message: '请填写房源详情', trigger: 'blur' },
            { min: 10, max: 200, message: '内容为10到200个字', trigger: 'blur' }
          ]
        }

      }
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },

      //分页查询的size属性(展示多少行)
      handleSizeChange(pageSize){
        this.queryInfo.pageSize = pageSize
        this.getHouseResourcesList()
      },
      //分页查询的number属性(第几页)
      handleCurrentChange(pageNum){
        this.queryInfo.pageNum = pageNum
        this.getHouseResourcesList()
      },

      //分页查询房源列表
      async getHouseResourcesList(){
          const {data: result} = await this.$http.get('/houseResources/findAll',{
           params: this.queryInfo
        })
        if(result.status !== 200) return this.$message.error("房源列表查询失败")
        this.$message.success("房源列表查询成功")
        this.houseResourceList = result.data.rows
        this.total = result.data.total
      },

      handleRemove(file, fileList) {
        console.log(file, fileList);
      },
      handlePictureCardPreview(file) {
        this.dialogImageUrl = file.url;
        this.dialogImageVisible = true;
      },

      //更新房源状态
      async updateStatus(houseResources) {
        const {data: result} = await this.$http.put("/houseResources/updateStatus", {
          id: houseResources.id,
          switchStatus: houseResources.switchStatus
        })
        // if (result.status !== 200) return this.$message.error("状态更新失败")
        // this.$message.success("状态更新成功")
        this.findStatus(houseResources)
      },

      //查询房源状态
      async findStatus(houseResources) {
        const {data: result} = await this.$http.get("/houseResources/findStatus?id="+houseResources.id)
        if (result.data.switchStatus===true){
          return this.$message.success("房源已上架")
        } else{
          this.$message.warning("房源已下架")
        }
      },

      // 弹出修改对话框，并回显当前房源数据
      async updateUserBtn(houseResources){
        this.dialogUpdateVisible=true
        const {data: result} = await this.$http.get("/houseResources/findHouse?id="+houseResources.id)
        this.updateHouseResources=result.data
      },

      async updateHouseResourcesBtn(){
        const {data: result} = await this.$http.put("/houseResources/updateHouseResource",this.updateHouseResources)
        if(result.status!==200) return this.$message.erroe("房源更新失败")
        this.$message.success("房源更新成功")
        this.dialogUpdateVisible=false
        this.getHouseResourcesList()
      }
      
    },
    mounted(){
        this.getHouseResourcesList();
    }
  }
</script>

<style>
    .demo-table-expand {
    font-size: 0;
  }

  .demo-table-expand label {
    width: 90px;
    color: #99a9bf;
  }

  .demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
  }

  .el-pagination {
      text-align: center;
  }
  
   
</style>